package com.vue.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.vue.api.entity.Menu;
import com.vue.api.entity.Page;
import com.vue.api.entity.PageResult;
import com.vue.api.entity.Result;
import com.vue.api.entity.Role;
import com.vue.api.entity.User;
import com.vue.api.service.LoginService;
import com.vue.api.util.PageUtil;

@RestController
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private LoginService loginService;

	@RequestMapping("login")
	public Result login(User user,HttpServletRequest request) {
		try {
			String parameter = request.getHeader("Authorization");
			String token="123456789";
			System.out.println(parameter);
//			try   
//			{   
//				Thread.currentThread().sleep(2000);//毫秒   
//			}   
//			catch(Exception e){
//
//			}  
			if(StringUtils.isEmpty(parameter)||"null".equals(parameter)) {
				System.out.println(parameter);

				System.out.println(user);
				User user1 = loginService.checkUser(user);
				System.out.println(user1);
				if(user1==null) {
					return new Result(Result.ERROR, "用户不存在", ""); 
				}
				if(!user.getPassWord().equals(user1.getPassWord())) {
					return new Result(Result.ERROR, "密码错误", ""); 	
				}

				return new Result(Result.SUCCESS, "登录成功", token); 		
			}else if("123456789".equals(parameter)){
				return new Result(Result.SUCCESS, "通过token登录成功", token); 
			}else if(!"123456789".equals(parameter)){
				return new Result(Result.ERROR, "token有误", ""); 
			}
			return new Result(Result.ERROR, "登录有误", "");
		} catch (Exception e) {
			// TODO: handle exception
			return new Result(Result.ERROR, "登录有误", "");
		}

	}

	@RequestMapping("getAllMenu")
	public Result getAllMenu(String userName,HttpServletRequest request) {
		String parameter = request.getHeader("Authorization");
		if(!Objects.equals(parameter, "123456789")) {
			return new Result(Result.ERROR, "请先登录错误", null); 
		}
		List<Menu> menuList= loginService.getAllMenu();
		return new Result(Result.SUCCESS, "查询成功", menuList); 
	}

	@RequestMapping("getAllUser")
	public Result getAllUser(HttpServletRequest request,User user) {
		String parameter = request.getHeader("Authorization");
		//		if(!Objects.equals(parameter, "123456789")) {
		//			return new Result(Result.ERROR, "请先登录错误", null); 
		//		}
		Page<User> page =PageUtil.getVuePage(request);
		page= loginService.findByPage(page,user);
		return new Result(Result.SUCCESS, "查询成功", PageUtil.getVuePageData(page)); 
	}
	
	@RequestMapping("updateUserState")
	public Result updateUserState(@RequestBody User user) {
		System.out.println(JSON.toJSONString(user));
		
//		if() {
//			return new Result(Result.ERROR, "更新失败", null); 
//		}
	    int count=loginService.updatUserState(user);
		return new Result(Result.SUCCESS, "更新成功", null); 
	}
	
	@RequestMapping("checkUserName")
	public Result checkUserName(User user) {
		System.out.println(JSON.toJSONString(user));
		User checkUser = loginService.checkUser(user);
		if(checkUser==null) {
			return new Result(Result.SUCCESS, "查询成功", true); 
		}
		return new Result(Result.SUCCESS, "更新成功", false); 
	}

	
	@RequestMapping("addUser")
	public Result addUser(@RequestBody User user) {
		System.out.println(JSON.toJSONString(user));
		int count=loginService.addUser(user);
		if(count==1) {
			return new Result(Result.SUCCESS, "新增成功", null); 
		}
		return new Result(Result.ERROR, "新增失败", null); 
	}
	
	
	@RequestMapping("findUserById")
	public Result findUserById(String id) {
		System.out.println(JSON.toJSONString(id));
		User user=loginService.findUserById(id);
	    if(user==null) {
	    	return new Result(Result.ERROR, "此用户不存在", null); 
	    }
		return new Result(Result.SUCCESS, "新增成功", user); 
	}
	
	@RequestMapping("getAllRoleMenu")
	public Result getAllRoleMenu() {
		List<Role> allRoleMenu = loginService.getAllRoleMenu();
		return new Result(Result.SUCCESS, "查询成功", allRoleMenu);
	}
	
	@RequestMapping("updateUser")
	public Result updateUser(@RequestBody User user) {
		System.out.println(JSON.toJSONString(user));
		int count=loginService.updateUser(user);
		if(count==1) {
			return new Result(Result.SUCCESS, "修改成功", null);
		}
		return new Result(Result.ERROR, "修改失败", null);
	}
	
	@RequestMapping("deleteUser")
	public Result deleteUser(String id) {
		System.out.println(id);
		int count=loginService.deleteUser(id);
		if(count==1) {
			return new Result(Result.SUCCESS, "删除成功", null);
		}
		return new Result(Result.ERROR, "删除失败", null);
	}



}
